﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class RoleMenuService : IRoleMenuService
    {
        Domain.Uow.IRepository<Domain.Entity.RoleMenu> roleMenuRespoitory;

        public RoleMenuService(Domain.Uow.IRepository<Domain.Entity.RoleMenu> roleMenu)
        {
            if (roleMenu == null)
            {
                throw new ArgumentNullException("roleMenuRespoitory is null");
            }
            roleMenuRespoitory = roleMenu;
        }

        public IQueryable<Domain.Entity.RoleMenu> GetList()
        {
            var res = roleMenuRespoitory.GetList().AsQueryable();
            return res;
        }

        public Domain.Entity.RoleMenu Add(Domain.Entity.RoleMenu roleMenu, Domain.Entity.User opeUser)
        {
            roleMenuRespoitory.Add(roleMenu);
            roleMenuRespoitory.Uow.Commit();
            return roleMenu;
        }

        public bool Delete(Domain.Entity.RoleMenu roleMenu, Domain.Entity.User opeUser)
        {
            var res = false;
            try
            {
                roleMenu.State = false;
                roleMenuRespoitory.Modify(roleMenu);
                roleMenuRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.RoleMenu roleMenu, Domain.Entity.User opeUser)
        {
            var res = false;
            try
            {
                roleMenuRespoitory.Delete(roleMenu);
                roleMenuRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User opeUser)
        {
            var res = false;
            var deleteList = roleMenuRespoitory.GetList(e => idList.Contains(e.Id));
            foreach (var item in deleteList)
            {
                item.State = false;
                roleMenuRespoitory.Modify(item);
            }
            try
            {
                roleMenuRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User opeUser)
        {
            var res = false;
            var delete = roleMenuRespoitory.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                roleMenuRespoitory.Delete(item);
            }
            try
            {
                roleMenuRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.RoleMenu roleMenu, Domain.Entity.User opeUser)
        {
            var res = false;
            roleMenuRespoitory.Modify(roleMenu);
            try
            {
                roleMenuRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public Domain.Entity.RoleMenu Get(int id)
        {
            var data = roleMenuRespoitory.Get(id);
            return data;
        }
    }
}
